package fr.cephb.socialsnp.core;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;

import fr.cephb.util.XObject;

@Entity
@Table(name="user")
@NamedQuery(
	    name="user.byLogin",
	    query="SELECT p FROM User p WHERE p.login = :login"
	    )
public class User
	extends XObject
	implements Serializable
	{

	private static final long serialVersionUID = 1L;
	
	public static final long ANONYMOUS_ID=2L;
	public static final User ANONYMOUS=new User(ANONYMOUS_ID,"anonymous",Role.ANONYMOUS);
	
	public static final long ROOT_ID=1L;
	public static final User ROOT=new User(ROOT_ID,"root",Role.ADMIN);
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private long id;
	@Column(nullable=false,name="login",unique=true,updatable=false)
	private String login;

	@Column(nullable=false,name="role")
	@Enumerated(value=EnumType.STRING)
	private Role role=Role.ANONYMOUS;

	
	public User()
		{
		}
	
	 @Id
     @Column(name="id")
     @GeneratedValue(strategy=GenerationType.AUTO)
     public int getId() {
             return id;
     }

     private void setId(int id) {
             this.id = id;
     }


	
	private void setId(long id) {
		this.id = id;
		}
	
	public long getId() {
		return id;
		}
		
	public String getLogin() {
		return login;
	}
	
	public void setLogin(String login) {
		this.login = login;
		}
	
	public Role getRole() {
		return role;
		}
	
	public void setRole(Role role) {
		this.role = role;
		}
	
	@Override
	public String toString() {
		return getLogin();
		}
	
	@Transient
	public boolean isIdentified()
		{
		return !(getRole()==null || getRole()==Role.ANONYMOUS);
		}

	

	
	}
